package boot.spring.service.impl;

import boot.spring.entity.Clock;
import boot.spring.entity.Department;
import boot.spring.mapper.ClockMapper;
import boot.spring.mapper.DepartmentMapper;
import boot.spring.service.ClockService;
import boot.spring.service.DepartmentService;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*用户打卡表的业务层实现类*/
@Service
public class ClockServiceImpl implements ClockService {
    /*获取当前年月日*/
    Date d = new Date();


    /*创建返回值对象*/
    Result r=new Result();

    @Autowired/*注入用户打卡表的持久层*/
    private ClockMapper clockMapper;
    @Autowired/*注入部门表的持久层*/
    private DepartmentMapper departmentMapper;



    /*分页多条件查询数据
     * 用户名查询
     * 签到状态查询
     * 签退状态查询
     * 打卡记录创建日期
     * */
    @Override
    public Result ClockSerAll(Integer page,Integer limit,String uname,String beginstate,String endstate,String clocktime) {

        Map<String,Object> map=new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        map.put("uname",uname);
        map.put("beginstate",beginstate);
        map.put("endstate",endstate);
        map.put("clocktime",clocktime);
        List<Map<String,Object>> all=clockMapper.ClockMapAll(map);
        if(all.size()>0){
            r=new Result(0,"请求成功",100,all);
        }else{
            r=new Result(1,"请求失败");
        }
        return r;
    }

    @Override /*根据用户id以及当前时间查询用户是否打卡签到*/
    public Result ClockSerAlltime(Clock alltime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        alltime.setClocktime(dateNowStr);/*把当前年月日存到实例化对象里面*/
        Clock clock=clockMapper.ClockMapAlltime(alltime);
        System.err.println("clock.getBeginstate()"+clock.getBeginstate());
        if(clock.getBeginstate()==null){
            r= new Result(1,    "未签到",1000,clock);
        }else{
            r= new Result(0,"已签到",1000,clock);
        }
        return r;
    }

    @Override/*修改用户签到数据*/
    public Result ClockSerAdd(Clock add) {
        /*获取到当前的年月日时分秒作为当前的签到时间*/
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr2 = sdf2.format(d);

        /*获取当前用户部门的上班时间*/
        Department dep=departmentMapper.DepMapAlltime(add.getUid());
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String dateNowStr = sdf.format(d);
        add.setSignintime(dateNowStr2);/*把当前时间填充到实例化对象里*/

        String[] time1 =dateNowStr.split(":");/*当前的时分*/
        String[] time2 =dep.getBeginworktime().split(":");/*数据库中的时分*/
        String state="";/*设置签到状态*/
        for(int i=0;i<time1.length;i++) {
            int date1 = Integer.parseInt(time1[i]);/*从获取的当前的时分截取当前的时分*/
            int date2 = Integer.parseInt(time2[i]);/*从数据库字符串中截取当前的时分*/
            if(date1==date2) {/*对比部门上班时间和现在打卡时间*/
                state="签到";
                continue;

            }else if(date1>date2){/*用户用户签到时间大于部门上班时间则签到状态为迟到*/
                state="迟到";
                break;
            }else if(date1<date2) {
                state="签到";
                break;
            }
        }
        add.setBeginstate(state);/*最后把用户打卡的状态填充到实例化对象里面*/
        Integer a=clockMapper.ClockMapAdd(add);
        if(a>0){
            r= new Result(0,"签到成功");
        }else{
            r= new Result(1,"签到失败");
        }
        return r;
    }

    @Override/*修改用户签退数据*/
    public Result ClockSerUp(Clock up) {

        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr3 = sdf3.format(d);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        /*获取到当前的年月日时分秒作为当前的签退时间*/
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr2 = sdf2.format(d);

        /*获取当前用户部门的下班时间*/
        Department dep=departmentMapper.DepMapAlltime(up.getUid());
        /*查询出当前用户在今天的打卡信息*/
        up.setClocktime(dateNowStr3);
        Clock clock=clockMapper.ClockMapAlltime(up);
        System.out.println();
        up.setSignbacktime(dateNowStr2);
        LocalDateTime date = LocalDateTime .parse(clock.getSignintime(), formatter);/*把数据库获取到的数据转换成日期类型*/
        date = date.plusMinutes(30);/*给数据库的数据类型加上半个小时*/
        System.err.println("数据库的日期"+date);
        LocalDateTime date2 = LocalDateTime .parse(dateNowStr2, formatter);/*把当前时间转换成日期类型*/
        System.err.println("当前日期"+date2);
        int compareTo2 = date.compareTo(date2);/*判断数据库的日期加上半个小时是否大于当前日期*/
        if(compareTo2>0){
            /*如果大于当前日期则返回签到后半个小时内不能签退*/
            System.err.println("如果大于当前日期则返回签到后半个小时内不能签退"+compareTo2);
            r= new Result(1,"签到半个小时内不能签退");
        }else if(compareTo2==0){
            /*如果等于当前日期则返回签到后半个小时内不能签退*/
            System.err.println("如果等于当前日期则返回签到后半个小时内不能签退"+compareTo2);
            r= new Result(1,"签到半个小时内不能签退");
        }else if(compareTo2<0){
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String dateNowStr = sdf.format(d);
            String[] time1 =dateNowStr.split(":");/*当前的时分*/
            String[] time2 =dep.getEndworktime().split(":");/*数据库中的时分*/
            String state="";/*设置签到状态*/
            for(int i=0;i<time1.length;i++) {
                int date1 = Integer.parseInt(time1[i]);/*从获取的当前的时分截取当前的时分*/
                int date3 = Integer.parseInt(time2[i]);/*从数据库字符串中截取当前的时分*/
                if(date1==date3) {/*对比部门上班时间和现在打卡时间*/
                    state="签退";
                    continue;

                }else if(date1>date3){/*用户用户签到时间大于部门上班时间则签到状态为迟到*/
                    state="签退";
                    break;
                }else if(date1<date3) {
                    state="早退";
                    break;
                }
            }
            up.setEndstate(state);
            Integer a=clockMapper.ClockMapUp(up);
            if(a>0){
                r= new Result(0,"签退成功");
            }else{
                r= new Result(1,"签退失败");
            }
        }
        return r;
    }


}
